package com.zrrd.gmall.list.receiver;

import com.rabbitmq.client.Channel;
import com.zrrd.gmall.list.service.SearchService;
import com.zrrd.gmall.rabbit.constant.MqConstant;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

//监听MQ消息
@Component
public class ListReceiver {
    @Autowired
    private SearchService searchService;

    //商品上架消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstant.QUEUE_GOODS_UPPER,durable = "true"),
            exchange = @Exchange(value = MqConstant.EXCHANGE_DIRECT_GOODS,
            type = ExchangeTypes.DIRECT,durable = "true"),
            key = {MqConstant.ROUTING_GOODS_UPPER}))
    public void upperGoods(Long skuId, Message message , Channel channel) throws IOException {
        System.out.println("搜索服务收到上架消息===>"+skuId);
        if (skuId != null){
            //更新es的数据
            searchService.upperGoods(skuId);
        }
        //消息的回执
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //商品下架消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstant.QUEUE_GOODS_LOWER,durable = "true"),
            exchange = @Exchange(value = MqConstant.EXCHANGE_DIRECT_GOODS,
                    type = ExchangeTypes.DIRECT,durable = "true"),
            key = {MqConstant.ROUTING_GOODS_LOWER}))
    public void lowerGoods(Long skuId, Message message , Channel channel) throws IOException {
        System.out.println("搜索服务收到下架消息===>"+skuId);
        if (skuId != null){
            //更新es的数据
            searchService.lowerGoods(skuId);
        }
        //消息的回执
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
